package com.sun.TrapDoor;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.Sun.DB.DBHelper;
import com.Sun.Model.encryptedIndex;
import com.Sun.index.buildIndex;
import com.Sun.index.indexEncrypted;
import com.Sun.index.indexHelper;


public class GenTropDoor {
	/**
	 * 为list中的关键字生成加密的陷门
	 * @param wordList
	 * @return
	 */
	public static encryptedIndex getTropDoor(List<String>wordList)
	{
		indexEncrypted inSK=buildIndex.getEncryptedSK();
		long startTime=System.currentTimeMillis();
		encryptedIndex resultTrapDoor=new encryptedIndex();
		double []trapdoorPlain=getSearchPlainIndex(wordList);
		encryptedIndex enIndex=EncryptedTroopHelper.encrypTropDoor(inSK,trapdoorPlain);
		resultTrapDoor.setMD1(enIndex.getMD1());
		resultTrapDoor.setMD2(enIndex.getMD2());
		long endTime=System.currentTimeMillis();
		System.out.println("生成TrapDoor的时间是："+(endTime-startTime)+"ms");
		return resultTrapDoor;
	}

	/**
	 * 得到wordlist中关键字对应的明文陷门
	 * @param wordList
	 * @return
	 */
	public static double [] getSearchPlainIndex(List<String>wordList)
	{
		long startTime=System.currentTimeMillis();
		Map<String, Double>idfMaps=DBHelper.getIDFSForWords(wordList);
		long endTime=System.currentTimeMillis();
		System.out.println("得到搜索关键字集合idf的时间是："+(endTime-startTime)+"ms");
		int disWordCount=indexHelper.wordDisCount;
		int n=disWordCount;
		double []resultIndex=new double[n];
		for(int i=0;i<n;i++)
		{
			resultIndex[i]=0;
		}
		double norIdfs=indexHelper.getNorValues(idfMaps);
		Iterator<String> iterator=wordList.iterator();
		while(iterator.hasNext())
		{
			String tempWord=iterator.next();
			int index=indexHelper.wordList.indexOf(tempWord);
			resultIndex[index]=idfMaps.get(tempWord)/norIdfs;//得到量化的ifd值
		}
		return resultIndex;
	}

}
